//
//  PublicProductsView.m
//  SUZHOUBANK
//
//  Created by saberLily on 16/4/11.
//  Copyright © 2016年 RuiFeng-WGCC. All rights reserved.
//

#import "PublicProductsView.h"
#import "HeaderView.h"
#import "PubScrollView.h"
#import "BaseAPIManager.h"
#import "DetailBottomTable.h"
@interface PublicProductsView()<HeaderViewDelegate>
{
    NSArray *_pubDataKey;
    NSMutableDictionary *_pubDataDic;
    
    NSMutableDictionary *_allRequestDataDic;
}
@property (nonatomic,strong) HeaderView *leftView;
@property (nonatomic,strong) PubScrollView *topScrollView;
@property (nonatomic,strong) HeaderView *rightView;
@property (nonatomic,strong) DetailBottomTable *bottomTableView; /**< 底部table */
@property (nonatomic,strong) UIWebView *webView; /**< 底部web */

@end
@implementation PublicProductsView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initializeDataSource];
        [self initializeUserInterface];
        [self getProAPIRequest];
    }
    return self;
}
#pragma mark - initializeDataSource 初始化数据源
-(void)initializeDataSource{
    _pubDataKey = @[@"企业网银",@"公司账户服务类",@"代理收付类",@"公司理财类",@"国际业务",@"公司融资类—传统类融资",@"公司存款类",@"公司结算类"];
    _pubDataDic = [NSMutableDictionary dictionary];
    _allRequestDataDic = [NSMutableDictionary dictionary];
    
    NSArray *valueArr = @[@[@"企业网银演示"],
                          @[@"企业资信证明"],
                          @[@"代发工资业务",@"房地产项目资本金托管"],
                          @[@"对公定制理财"],
                          @[@"择期结售汇",@"远期结售汇",@"即期结售汇",@"融资性对外担保",@"对外保函",@"进口代收项下获取质押受信",@"未来货权质押开证",@"福费延",@"外币票据贴现",@"出口托收押汇",@"信用证项下出口押汇",@"打包贷款",@"进口代付",@"提货担保",@"进口代收押汇",@"信用证项下出口押汇",@"进口开证",@"外币固定资产贷款",@"外币流动资金贷款"],
                          @[@"商业承兑汇票保贴",@"厂商银",@"保兑仓"],
                          @[@"单位协议存款",@"单位协定存款",@"单位通知存款",@"单位定期存款",@"单位活期存款"],
                          @[@"苏付通",@"上门服务",@"单位人民币结算卡",@"委托收款",@"汇款",@"华东三省",@"银行本票",@"转账支票",@"现金支票"]
                          ];
    
    [_pubDataKey enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [_pubDataDic setObject:valueArr[idx] forKey:obj];
    }];
}

-(void)getProAPIRequest{
    [BaseAPIManager requestURL:@"protocol_company_product!list.action" parameters:@{@"pageCount":@1} completion:^(bool success, NSInteger status, NSDictionary *data, NSError *error) {
        if (success) {
            [data[@"datalist"] enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                [_allRequestDataDic setObject:obj[@"id"] forKey:obj[@"cpmc"]];
//                 NSLog(@"%@",obj[@"cpmc"]);
            }];
   
        }
    }];
    
}

#pragma mark - initializeUserInterface 初始化用户界面
-(void)initializeUserInterface{
    [self addSubview:self.topScrollView];
    
    [self addSubview:self.bottomTableView];
    
    [self addSubview:self.leftView];
    [self addSubview:self.rightView];
    

}


-(void)getProductsWithID:(NSString *)proID{
    [BaseAPIManager requestURL:@"protocol_company_product!detail.action" parameters:@{@"id":proID} completion:^(bool success, NSInteger status, NSDictionary *data, NSError *error) {
        if (success) {
            
            NSDictionary *titleDic = @{@"cpmc":@"产品名称",@"cpjs":@"产品介绍",@"cptd":@"产品特点",@"bltj":@"办理条件",@"jbys":@"基本要素",@"shdx":@"适合对象",@"tjzl":@"提交资料",@"bllc":@"办理流程"};
            
            NSMutableArray *keyArr = [NSMutableArray array];
            
            NSMutableDictionary *arrDic = [NSMutableDictionary dictionary];
            for (id obj in [titleDic allKeys]) {
                if (data[obj] && ![data[obj] isEqualToString:@""]) {
                    
                    [keyArr addObject:titleDic[obj]];
                    [arrDic setObject:data[obj] forKey:titleDic[obj]];

                }
            }
            self.bottomTableView.dataSource = keyArr;
            self.bottomTableView.dataSourceDic = arrDic;
            
            [self.bottomTableView.tableView reloadData];
            
            if (self.bottomTableView) {
                [self.bottomTableView.tableView reloadData];

            }else{
//            [self addSubview:self.bottomTableView];
            }
        }
    }];
}

#pragma mark *** delegate ***
-(void)HeadView:(HeaderView *)headView didSelectedRow:(NSInteger)indexPathRow didSelectedHeader:(BOOL)selectedHeader dataCount:(NSInteger)count{
    if (headView == self.leftView) {
        self.rightView.dataSource = _pubDataDic[_pubDataKey[indexPathRow]];
        [self.rightView reloadTableViewData];
        
        [self.rightView.headerBtn setTitle:_pubDataDic[_pubDataKey[indexPathRow]][0] forState:UIControlStateNormal];
        
    }else{
        
    }
    
    NSString *proName = [self.rightView.headerBtn titleForState:UIControlStateNormal];
    if ([proName isEqualToString:@"企业网银演示"]) {
        if (self.bottomTableView.tableView) {
            [self.bottomTableView.tableView removeFromSuperview];
        }
        [self.bottomTableView addSubview:self.webView];
     
    }else{
        if (self.webView) {
            [self.webView removeFromSuperview];
            
        }
    [self.bottomTableView addSubview:self.bottomTableView.tableView];
    [self getProductsWithID:_allRequestDataDic[proName]];
    }

        CGRect leftOriginFrame = CGRectMake(30, CGRectGetMaxY(self.topScrollView.frame), 350, 40*count);
        CGRect rightOringFrame = CGRectMake(CGRectGetMaxX(self.leftView.frame)+20, CGRectGetMaxY(self.topScrollView.frame), CGRectGetWidth(self.bounds)-CGRectGetMaxX(self.leftView.frame)-40, 40*count);
    if (headView == self.leftView) {
        if (!selectedHeader) {
            [UIView animateWithDuration:0.35f animations:^{
                self.leftView.frame = CGRectMake(30, CGRectGetMaxY(self.topScrollView.frame), 350, 40);
            }];
            
        }else{
            [UIView animateWithDuration:0.35f animations:^{
                self.leftView.frame = leftOriginFrame;
            }];
        }
    }else{
        if (!selectedHeader) {
            [UIView animateWithDuration:0.35f animations:^{
                self.rightView.frame = CGRectMake(CGRectGetMaxX(self.leftView.frame)+20, CGRectGetMaxY(self.topScrollView.frame), CGRectGetWidth(self.bounds)-CGRectGetMaxX(self.leftView.frame)-40, 40);
            }];
    
        }else{
            [UIView animateWithDuration:0.35f animations:^{
                self.rightView.frame = rightOringFrame;
            }];
        }
    }
}


-(void)HeadView:(HeaderView *)headView didSelectedHeader:(BOOL)selectedHeader{
        CGRect leftOriginFrame = CGRectMake(30, CGRectGetMaxY(self.topScrollView.frame), 350, 300);
        CGRect rightOringFrame = CGRectMake(CGRectGetMaxX(self.leftView.frame)+20, CGRectGetMaxY(self.topScrollView.frame), CGRectGetWidth(self.bounds)-CGRectGetMaxX(self.leftView.frame)-40, 300);
    
    if (headView == self.leftView) {
        if (!selectedHeader) {
            [UIView animateWithDuration:0.35f animations:^{
                self.leftView.frame = CGRectMake(30, CGRectGetMaxY(self.topScrollView.frame), 350, 40);
            }];
            
        }else{
            [UIView animateWithDuration:0.35f animations:^{
                self.leftView.frame = leftOriginFrame;
            }];
        }
    }else{
        if (!selectedHeader) {
            [UIView animateWithDuration:0.35f animations:^{
                self.rightView.frame = CGRectMake(CGRectGetMaxX(self.leftView.frame)+20, CGRectGetMaxY(self.topScrollView.frame), CGRectGetWidth(self.bounds)-CGRectGetMaxX(self.leftView.frame)-40, 40);
            }];
            
        }else{
            [UIView animateWithDuration:0.35f animations:^{
                self.rightView.frame = rightOringFrame;
            }];
        }
    }
}
   
#pragma mark *** getters ***
-(PubScrollView *)topScrollView{
    if (!_topScrollView) {
        _topScrollView = [[PubScrollView alloc] initWithFrame:CGRectMake(30, 20, CGRectGetWidth(self.bounds)-50, 300) ScrollStyle:ScrollStyleDefault];
        _topScrollView.imageUrlArr = [@[@"对公产品_0",@"对公产品_1",@"对公产品_2"] mutableCopy];
        
        [_topScrollView initializeUserInterface];
        
    }
    return _topScrollView;
}
-(HeaderView *)leftView{
    if (!_leftView) {
        _leftView = [[HeaderView alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(self.topScrollView.frame), 350, 300)];
        _leftView.dataSource = _pubDataKey;
        [_leftView setHeaderTitle];
        _leftView.delegate = self;
    }
    return _leftView;
}
-(HeaderView *)rightView{
    if (!_rightView) {
        _rightView = [[HeaderView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.leftView.frame)+20, CGRectGetMaxY(self.topScrollView.frame), CGRectGetWidth(self.bounds)-CGRectGetMaxX(self.leftView.frame)-40, 300)];
        [_rightView.headerBtn setBackgroundImage:[UIImage imageNamed:@"对公产品_11"] forState:UIControlStateNormal];
        _rightView.dataSource = @[@"企业网银演示"];
        [_rightView setHeaderTitle];
        _rightView.delegate = self;
    }
    return _rightView;
}
-(DetailBottomTable *)bottomTableView{
    if (!_bottomTableView) {
        _bottomTableView = [[DetailBottomTable alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(self.topScrollView.frame)+60,  CGRectGetWidth(self.bounds), 800)];
        _bottomTableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"对公产品背景"]];
        
        [_bottomTableView addSubview:self.webView];
        
    }
    return _bottomTableView;
}
-(UIWebView *)webView{
    if (!_webView) {
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds)-60, 500)];
        _webView.layer.cornerRadius = 10.0f;
        _webView.layer.masksToBounds = YES;
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://ebank.suzhoubank.com/corporplat/"]];
        [_webView loadRequest:request];
    }
    return _webView;
}
@end
